<style type="text/css">
    .table th a, .table td a.submitdelete {
        color: #FF0000 !important; display: inline;
    }
    .table th a:hover, .table td a.submitdelete:hover {
        color: #FFFFFF !important;
        text-decoration: none !important;
    }
    .form-table input {
        width: 210px;
    }
    .column-thumbnail{
        background: #FFF !important
    }
    td.column-thumbnail{
        width: 80px !important;
        text-align: center
    }
</style>
<div class="container">

    <div class="page-header">
        <div class="row">
            <h1 class="span8">Document :: <?= ucfirst($document['title']) ?></h1>
            <span class="span4 clearfix">
                <a class="nav-control new-page w-18" href="<?= base_url('documents/view_new?post_type=document') ?>">
                    Add Folders / Files
                </a>
            </span>
        </div>


    </div>

    <div class="row">

        <form accept-charset="utf-8" action="<?= base_url() ?>documents/submit_gallery" method="POST" id="updategallery_form">

            <input type="hidden" value="edit-gallery" name="post_action" id="post_action">
            <input type="hidden" value="<?= $document['ID'] ?>" name="document_ID">

            <div class="span12 wrap ui-tabs ui-widget ui-widget-content" style="border: 0; padding: 0;">

                <div class="ui-tabs-panel ui-widget-content ui-corner-all" style="border-width: 1px; border-color: #DADADA; padding: 15px 15px 23px">

                    <h2>Document settings</h2>

                    <table class="form-table"> 
                        <tbody>


                            <tr valign="top"> 

                                <td class="upload_text">Title:</td> 

                                <td>

                                    <input type="text" value="<?= $document['title'] ?>" name="galleryname" size="43">

                                    <i>( Use characters: a-z, A-Z, 0-9, _ )</i>

                                </td>

                            </tr>

                            <tr valign="top"> 

                                <td class="upload_text">Link to:</td> 

                                <td>       



                                    <?
                                    $_GET['_cur_level'] = ''; 

                                    $this->firephp->log($document['posts_ID']);

                                    $_GET['myPost']['gallery_ID']=$document['posts_ID'];

                                    function parents_finder ($parent_ID = 0, $page_type){

                                    $CI = & get_instance ();

                                    $myPosts = $CI->posts_model->view_all_children($parent_ID,$page_type);


                                    foreach ($myPosts as $onePost):
                                    $selected = '';

                                    $cur_ID = $onePost['ID'];

                                    if ($cur_ID == $_GET['myPost']['gallery_ID'])
                                    $selected = 'selected="selected"';

                                    ?><option value="<?= $cur_ID ?>" <? echo $selected; ?>><?= repeat_sign('&nbsp;', $_GET['_cur_level'] * 3) . '- ' . $onePost['name'] ?></option><?

                        if(count($CI->posts_model->view_all_children($cur_ID,$page_type))>0):

                        $_GET['_cur_level'] ++;
                        parents_finder($cur_ID, $page_type);

                        endif;

                        endforeach;

                        $_GET['_cur_level'] --;

                        }

                        ?>

                        <select id="linkto" name="linkto">
                            <option value="0" selected="selected">--Please select </option>

                            <? foreach (config_item('page_types') as $page_type): ?>
                            <optgroup label="<?= ucfirst($page_type); ?>">
                                <?= parents_finder(0, $page_type); ?>
                                <? endforeach; ?>
                        </select>

                        <?
                        unset($_GET['myPost']);
                        unset($_GET['_cur_level']);
                        ?>



                        </td> 

                        </tr>

                        <tr valign="top"> 

                            <td class="upload_text">Parent:</td> 

                            <td>           



                                <?
                                $_GET['_cur_level'] = ''; 
                                $_GET['myPost']['gallery_ID'] = $_GET['documents_ID']; 
                                $_GET['myPost']['parent_ID'] = $document['parent_ID']; 

                                function parents_finder2 ($parent_ID = 0, $page_type){

                                $CI = & get_instance ();

                                $galleries = $CI->document_model->view_all_children($parent_ID, $page_type);

                                foreach ($galleries as $oneGallery):
                                $selected = '';

                                $cur_ID = $oneGallery['ID'];

                                if ($cur_ID != $_GET['myPost']['gallery_ID']):

                                if ( $cur_ID == $_GET['myPost']['parent_ID'])
                                $selected = 'selected="selected"';


                                ?>
                        <option value="<?= $cur_ID ?>"  <? echo $selected; ?>><?= repeat_sign('&nbsp;', $_GET['_cur_level'] * 3) . '- ' . $oneGallery['title'] ?></option>
                        <?
                        if(count($CI->document_model->view_all_children($cur_ID, $page_type))>0):

                        $_GET['_cur_level'] ++;
                        parents_finder2($cur_ID, $page_type);

                        endif;

                        endif;
                        endforeach;

                        $_GET['_cur_level'] --;

                        }

                        ?>

                        <select id="parent_id" name="parent_id">
                            <option value="0" selected="selected">(no parent)</option>
                            <?foreach (config_item('page_types') as $page_type):?>
                            <optgroup label="<?= ucfirst($page_type); ?>">
                                <?= parents_finder2(0, $page_type); ?>
                                <? endforeach; ?>
                        </select>

                        <?
                        unset($_GET['_cur_level']);
                        ?>


                        <br>                
                        <br>

                        </td> 

                        </tr> 

                        </tbody>

                    </table>

                    <div class="submit row"> 

                        <span class="span2 ml-15> save2" style="width: 125px !important;"><input type="submit" value="Edit document" class="btn" id="edit_gallery" disabled="disabled"></span>
                        <span class="span4 loader"><img id="ajax-loading" class="ajax-loading" src="<?= base_url() ?>assets/img/backgnds/loading.gif" style="visibility: hidden;"></span>

                    </div>

                </div>

            </div>

        </form>

    </div>

    <br><br>

    <? if (count($files) > 0): ?>

    <form accept-charset="utf-8" action="<?= base_url() ?>documents/files_order" method="POST">
        <input type="hidden" value="<?= $document['ID'] ?>" name="document_ID">

        <h2> Files </h2>

        <div class="row">

            <div class="span12">

                <table class="table table-striped table-bordered table-condensed">
                    <thead>
                        <tr>
                            <th>Type</th>
                            <th>Filename</th>
                            <th class="center">Order</th>
                        </tr>
                    </thead>
                    <tbody>

                        <?
                        $k = 1;
                        foreach ($files as $file):
                        ?> 


                        <!-- HELPER  -->
                        <?
                        $pic_pices = explode('.', $file['name']);
                        $pic_title = $pic_pices[0];
                        $pic_arr = explode('.', $file['filename']);
                        $extension = $pic_arr[count($pic_pices)-1];
                        ?>

                        <tr>

                            <td class="column-thumbnail">
                                <a title="<?= $file['filename'] ?>" href="<?= HOST_URL_CMS ?>pro-documents/<?= $document['path'] ?>/<?= $file['filename'] ?>"  target="_blank">

                                    <img src="<?= base_url() ?>assets/icons/pro-documents/<?= $extension ?>.png" width="60" />

                                </a>
                            </td>
                            <td>
                                <div><strong><?= $file['name'] ?></strong></div>

                                <div><?= date('m.d.Y', $file['date_time']) ?></div>
                                <br>

                                <div>
                                    <a title="View &quot;<?= $file['name'] ?>&quot;" href="<?= HOST_URL_CMS ?>pro-documents/<?= $document['path'] ?>/<?= $file['filename'] ?>" class="shutter" target="_blank">View</a> | 

                                    <a onclick="javascript:check = confirm('Delete &quot;<?= $file['name'] ?>&quot; ?');
                                            if (check == false)
                                                return false;" href="<?= base_url() ?>documents/delete_file?picture_ID=<?= $file['ID'] ?>&document_ID=<?= $document['ID'] ?>" class="submitdelete">Delete</a>

                                </div>

                            </td>
                            <td class="column-order">

                                <input name="picture-order[]" value="<?= $file['order'] ?>" tabindex="<?= $k + 5 ?>" />

                            </td>

                        </tr>


                        <?
                        $k++;
                        endforeach;
                        ?>
                    </tbody>
                </table>

            </div>

        </div>

        <div class="form-actions">

            <div class="row">

                <span class="span4 save"><button class="btn btn-primary" type="submit"><i class="icon-white icon-download-alt"></i> Save changes</button></span>
                <span class="span4 loader"><img id="ajax-loading" class="ajax-loading" src="<?= base_url() ?>assets/img/backgnds/loading.gif" style="visibility: hidden;"></span>
            </div>

        </div>

    </form>

    <? endif; ?>

</div> <!-- /container -->
<script type="text/javascript">

    var post_type = '<?= $_GET['post_type'] ?>';
    var page = 'documents';

</script>